/*
 * about.c
 *
 *  Created on: 2012-6-8
 *      Author: Administrator
 */

#include "tangoc.h"

int bootloader_version()
{
	int i = 0, ret;
	union int_char ver;

	ioctl(gdev, DISABLE_IRQ, 0);
	for (i = 0; i < 5; i++)
	{
		ioctl(gdev, BOOTLOADER_MODE, 0);
		ret = read(gdev, ver.buf, 4);
		ioctl(gdev, RESET_TP, 0);
		msleep(100);
		if (ret == 4) break;
	}
	ioctl(gdev, ENABLE_IRQ, 0);
	if (ret != 4)
	{
		puts("bootloader version failed.");
		return 0;
	}
	printf("\nBootloader version: %x %x %x %x\n",
			ver.buf[3], ver.buf[2], ver.buf[1], ver.buf[0]);

	return ver.num;
}

int firmware_version()
{
	char wbuf = 62;
	int i = 0;
	union short_char ver;

	ioctl(gdev, DISABLE_IRQ, 0);
	ioctl(gdev, DEFAULT, 0);
	for (; i < 5; i++)
	{
		if (write(gdev, &wbuf, 1) != 1)
		{
			msleep(100);
			continue;
		}
		msleep(10);
		if (read(gdev, ver.buf, 2) == 2)
		{
			if (ver.buf[0] && ver.buf[1]) break;
		}
		msleep(100);
	}
	ioctl(gdev, ENABLE_IRQ, 0);
	printf("Firmware version: %x %x, ", ver.buf[1], ver.buf[0]);

	switch (ver.num)
	{
	case MP71:
		printf("MP7.1");
		break;
	case MP72:
		printf("MP7.2");
		break;
	case MP80:
		printf("MP8.0");
		break;
	case MP81:
		printf("MP8.1");
		break;
	case MP82:
		printf("MP8.2");
		break;
	case MP83:
		printf("MP8.3");
		break;
	case MP84:
		printf("MP8.4");
		break;
	case MP90:
		printf("MP9.0");
		break;
	case MP91:
		printf("MP9.1");
		break;
	case MP92:
		printf("MP9.2");
		break;
	case TM70:
		printf("Tune MP7.0");
		break;
	case TM80:
		printf("Tune MP8.0");
		break;
	case TM81:
		printf("Tune MP8.1");
		break;
	case TM82:
		printf("Tune MP8.2");
		break;
	default:
		printf("unknown");
		break;
	}
	printf("\n");

	return ver.num;
}

void slave_version()
{
	char wbuf = 64;
	char rbuf[32]="\0";
	int i = 0;
	ioctl(gdev, DISABLE_IRQ, 0);
	ioctl(gdev, DEFAULT, 0);
	for (; i < 5; i++)
	{
		if (write(gdev, &wbuf, 1) != 1)
		{
			msleep(100);
			continue;
		}
		msleep(100);
		if (read(gdev, rbuf, 32) == 32) break;
		msleep(100);
	}
	ioctl(gdev, ENABLE_IRQ, 0);
	printf("EEPROM version: ");
	for(i=0; i<32; i++)
	{
		printf("%c", rbuf[i]);
	}
	printf("\n");
}

void show_about()
{
	bootloader_version();
	firmware_version();
	slave_version();
	puts("Version: TangoC v2.1.3 beta3");
	puts("Author: Jessay Chen");
	puts("E-mail: zlchen@pixcir.com.cn");
	puts("Release time: Februray 28, 2012");
	puts("Note: only support pixcir TP with pixcir driver");
	puts("(c) Copyright pixcir microelectronics co., ltd");
}
